package edu.zzuli.test.controller;

import edu.zzuli.test.service.ThumbnailService;
import edu.zzuli.test.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created by Chen-D.W on 2018/3/5.
 */
@Controller("uploadController")
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ThumbnailService thumbnailService;

    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public ModelAndView uploadImage(@RequestParam("image") CommonsMultipartFile file, HttpSession session){
//        相对路径
        String uploadPath="/image";
//        获取主机上要存储图片的存储位置的绝对路径
        String realUploadPath=session.getServletContext().getRealPath(uploadPath);
        String imageUrl=uploadService.uploadImage(file,uploadPath,realUploadPath);
        String thumImageUrl=thumbnailService.thumbnail(file,uploadPath,realUploadPath);
        ModelAndView mv=new ModelAndView();
        mv.addObject("imageUrl",imageUrl);
        mv.addObject("thumImageUrl",thumImageUrl);
        mv.setViewName("index2");
        return mv;
    }
}
